import wepy from 'wepy'


export default class extends wepy.mixin {

  data = {
    // 收货地址空对象
    addressInfo: null,
    goodsInfo: [],
    indicatorDots: true,
    autoplay: true,
    interval: 3000,
    duration: 1000
  }

  onLoad(params) {
    this.getgoodsdetail(params.goods_id)
    this.addressInfo = wepy.getStorageSync('address') || null
  }

  computed = {
    // 计算商品数量
    allShopCount(){
      return this.$parent.globalData.total
    },
    addressIn() {
      if (this.addressInfo == null) return '请选择收货地址'
      var addr = this.addressInfo
      var str = `${addr.provinceName}${addr.cityName}${addr.countyName}${addr.detailInfo}`
      return str
    }
  }

  methods = {
    // 加入购物车
    addCart(){
       wepy.showToast({
        title: '加入购物车',
        icon: 'success',
        duration: 1500
       })
      this.$parent.addGoodsList(this.goodsInfo)
      // //console.log(this.goodsInfo)
      // //console.log(this===wepy)
      // //console.log(this);
      
    },
    // 选择地址
    async  chooseAddress() {
      var res = await wepy.chooseAddress().catch(err => err)
      // //console.log(res)
      if (res.errMsg != 'chooseAddress:ok') return wepy.baseToast('你取消了')
      this.addressInfo = res;
      wepy.setStorageSync('address', res)
      this.$apply();
    },

    previewImg(img) {
      wepy.previewImage({
        current: img, // 当前显示图片的http链接
        urls: this.goodsInfo.pics.map(i => i.pics_big)  // 需要预览的图片http链接列表
      })
    }
  }
  async getgoodsdetail(id) {
    var { data: res } = await wepy.get('/goods/detail', {
      goods_id: id
    })
    if (res.meta.status != 200)
      return wepy.baseToast();
    this.goodsInfo = res.message
    this.$apply()
    ////console.log(this.goodsInfo);

  }
}